<?php

namespace app\web\service;

use app\common\logic\WxAppLogic;
use app\common\model\trUser;
use app\common\util\ErrorCode;

/**
 * Class Auth
 * @package app\admin\service
 * 用户服务类
 */
class UserService
{
    private $returnData;
    private $params;

    public function __construct($data, $params)
    {
        $this->returnData = $data;
        $this->params = $params;
    }

    //获取登录用户基本信息
    public function getInfo(){
        $memberModel = new trUser();
        $field = "id,number_id,nickname,gender,mobile,email,avatar,openid,balance,amount,address,remark,register_time,user_type,share_qr";
        $data = $memberModel->getFields($field, ["id" => request()->userLoginData["id"]], true, false);
        if(!empty($data)) {
            $data["userType"] = intval($data["userType"]);
            if (!$data['shareQr']){
                if ($wxQrCodeUrl = WxAppLogic::createQRCode(request()->userLoginData["id"])){ // 保存
                    $res = $memberModel->doUpdate(['share_qr' => $wxQrCodeUrl['url']],['id' => request()->userLoginData["id"]]);
                    if(!$res){
                        $this->returnData = ErrorCode::$errorCode["fail"];
                    }
                    $data['shareQr'] = $wxQrCodeUrl['url'];
                }
            }
        }
        $this->returnData["data"] = $data;
        return $this->returnData;
    }
}